//
//  HDForgetPasswordViewController.m
//  HDecoration
//
//  Created by 曹群 on 15/8/15.
//  Copyright (c) 2015年 HDecoration. All rights reserved.
//

#import "HDForgetPasswordViewController.h"

@interface HDForgetPasswordViewController ()
@property(nonatomic,strong)UIImageView *backgroundImage;
@property(nonatomic,strong)HDTextField *phoneNumberField;
@property(nonatomic,strong)HDTextField *verifyCodeField;
@property(nonatomic,strong)HDTextField *aNewPasswordField;
@property(nonatomic,strong)UIButton *verifyBtn;
@property(nonatomic,strong)UIButton *loginBtn;
@property NSTimer* time;
@end

@implementation HDForgetPasswordViewController
@synthesize verifyBtn;

- (void)viewDidLoad {
    self.view.backgroundColor = kContentViewColor;
    [super viewDidLoad];
    [self.view addSubview:self.backgroundImage];
    [self.backgroundImage addSubview:self.phoneNumberField];
    [self.backgroundImage addSubview:self.verifyCodeField];
    [self.backgroundImage addSubview:self.aNewPasswordField];
    [self.backgroundImage addSubview:self.loginBtn];
    [[NSNotificationCenter defaultCenter]addObserver:self
                                            selector:@selector(checkInputFinished)
                                                name:UITextFieldTextDidChangeNotification object:nil];
}

- (void)checkInputFinished
{
    self.loginBtn.enabled = (self.phoneNumberField.text.length&&self.verifyCodeField.text.length&&self.aNewPasswordField.text.length);
}

- (void)loginAction:(UIButton *)sender
{
    [self.view endEditing:YES];
    
    if (![HDUtil checkPassword:self.aNewPasswordField.text]) {
        [netEngine showErrorAlertWithMsg:@"密码格式不正确，6-16位的字母、数字或符号的组合"];
        return;
    }
    
    NSDictionary* params=@{
                           @"mobile_no":self.phoneNumberField.text,
                           @"password":self.aNewPasswordField.text,
                           @"verify_code":self.verifyCodeField.text
                           };
    
    [netEngine RequestForgetPasswordWithTarget:self.VctrID hudView:self.backgroundImage Data:params];
}

-(void)updateViewController:(NSNotification *)note
{
    NSDictionary* dic=note.userInfo;
    if ([dic objectForKey:Receiver]==self.VctrID)
    {
        if (dic[@"forgetPassword"]!=nil)
        {
           [self.navigationController popViewControllerAnimated:YES];
        }
        if (dic[@"VerifyCode"]!=nil)
        {
            if (self.time)
            {
                [self.time invalidate];
                self.time=nil;
            }
            self.verifyBtn.tag=60;
            self.verifyBtn.enabled=NO;
            self.time=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
        }
    }
}


- (void)verifyBtnAction:(UIButton *)sender
{
    NSDictionary* params=@{
                           @"mobile_no":self.phoneNumberField.text,
                           };
    [netEngine RequestVerifyCodeWithTarget:self.VctrID hudView:self.backgroundImage Data:params];
}

-(void)timerAction
{
    self.verifyBtn.tag-=1;
    if (self.verifyBtn.tag<=0)
    {
        self.verifyBtn.enabled=YES;
        [self.verifyBtn setTitle:@"验证" forState:UIControlStateNormal];
    }
    else
    {
        [self.verifyBtn setTitle:[NSString stringWithFormat:@"%zis",self.verifyBtn.tag] forState:UIControlStateNormal];
    }
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    [self.view endEditing:YES];
}

#pragma mark - getters
- (UIImageView *)backgroundImage
{
    if (!_backgroundImage) {
        _backgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 74, APPFRAME.size.width-20, (APPFRAME.size.width-20)*703/600.0)];
        UIImage *image = [UIImage imageNamed:@"忘记密码背景"];
        image = [image stretchableImageWithLeftCapWidth:0 topCapHeight:_backgroundImage.height-10];
        _backgroundImage.image = image;
        _backgroundImage.layer.masksToBounds = YES;
        _backgroundImage.userInteractionEnabled = YES;
    }
    return _backgroundImage;
}

- (HDTextField *)phoneNumberField
{
    if (!_phoneNumberField) {
        _phoneNumberField = [[HDTextField alloc] initWithFrame:CGRectMake(10, 20, self.backgroundImage.width-20, 44)icon:@"号码" title:nil];
        _phoneNumberField.layer.borderColor = [UIColor colorWithRed:0.8824 green:0.8824 blue:0.8824 alpha:1].CGColor;
        _phoneNumberField.layer.borderWidth = 0.5;
        _phoneNumberField.layer.cornerRadius = 5;
        _phoneNumberField.placeholder = @"请输入手机号码";
        _phoneNumberField.font = [UIFont systemFontOfSize:15];
        UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 44)];
        UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(0, 5, 0.5, 34)];
        line.image = [UIImage imageNamed:@"dot"];
        self.verifyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        verifyBtn.frame = CGRectMake(10, 7, 60,30);
        [verifyBtn setBackgroundImage:[UIImage imageNamed:@"验证"] forState:UIControlStateNormal];
        [verifyBtn setTitle:@"验证" forState:UIControlStateNormal];
        [verifyBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        verifyBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [self.verifyBtn addTarget:self action:@selector(verifyBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [rightView addSubview:line];
        [rightView addSubview:verifyBtn];
        _phoneNumberField.rightViewMode = UITextFieldViewModeAlways;
        _phoneNumberField.rightView = rightView;
        _phoneNumberField.delegate = (id)self;
    }
    return _phoneNumberField;
}

- (HDTextField *)verifyCodeField
{
    if (!_verifyCodeField) {
        _verifyCodeField = [[HDTextField alloc] initWithFrame:CGRectMake(10, self.phoneNumberField.bottom+5, self.backgroundImage.width-20, 44)icon:@"密码" title:nil];
        _verifyCodeField.layer.borderColor = [UIColor colorWithRed:0.8824 green:0.8824 blue:0.8824 alpha:1].CGColor;
        _verifyCodeField.layer.borderWidth = 0.5;
        _verifyCodeField.layer.cornerRadius = 5;
        _verifyCodeField.placeholder = @"请输入验证码";
        _verifyCodeField.font = [UIFont systemFontOfSize:15];
        _verifyCodeField.delegate = (id)self;
    }
    return _verifyCodeField;
}

- (HDTextField *)aNewPasswordField
{
    if (!_aNewPasswordField) {
        _aNewPasswordField = [[HDTextField alloc] initWithFrame:CGRectMake(10, self.verifyCodeField.bottom+5, self.backgroundImage.width-20, 44)icon:@"密码" title:nil];
        _aNewPasswordField.layer.borderColor = [UIColor colorWithRed:0.8824 green:0.8824 blue:0.8824 alpha:1].CGColor;
        _aNewPasswordField.layer.borderWidth = 0.5;
        _aNewPasswordField.layer.cornerRadius = 5;
        _aNewPasswordField.secureTextEntry=YES;
        _aNewPasswordField.placeholder = @"请输入新密码";
        _aNewPasswordField.font = [UIFont systemFontOfSize:15];
        _aNewPasswordField.delegate = (id)self;
    }
    return _aNewPasswordField;
}

- (UIButton *)loginBtn
{
    if (!_loginBtn) {
        _loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _loginBtn.frame = CGRectMake(15, self.aNewPasswordField.bottom+15, self.backgroundImage.width-20, 44);
        [_loginBtn setBackgroundImage:[UIImage imageNamed:@"登录"] forState:UIControlStateNormal];
        [_loginBtn setTitle:@"去登录" forState:UIControlStateNormal];
        _loginBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [_loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_loginBtn addTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside];
        _loginBtn.enabled = NO;
    }
    return _loginBtn;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
